﻿using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
namespace XiaoSe.IO
{
    public class ObjectToByte
    {
        public byte[] GetByte(object obj)
        {
            IFormatter formatter = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            formatter.Serialize(ms, obj);
            byte[] bObject = ms.GetBuffer();
            ms.Close();
            return bObject;
        }
        public object GetObject(byte[] bObject)
        {
            IFormatter formatter = new BinaryFormatter();
            MemoryStream ms = new MemoryStream(bObject);
            object obj = formatter.Deserialize(ms);
            ms.Close();
            return obj;
        }
    }
}
